#region (c) 2014 THN Solutions LLC. - All rights reserved
/*
THN MVC MODULE FOR THN WEB SERVER

Copyright (c) 2014, THN Solutions LLC. ( www.ThnSolutions.com )
Author: Nguyễn, M. Hải                 ( www.Minh-Hai.com     )
All rights reserved.

This library is dual-licensed.
  + For use in commercial software, please obtain a commercial license from THN Solutions LLC.    

  + For use in free software, this library is licensed under GPL version 3. A summary of 
	GPLv3 is listed below. You should also find a copy of GPLv3 as file License_GPLv3.txt 
	included with the source-code files.

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License version 3 as published by
    the Free Software Foundation  of the License.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
#endregion

#region Record of Change
//             							CHANGE HISTORY
// -------------------------------------------------------------------------------------
// |   DATE    | DEVELOPER  | DESCRIPTION                                              |
// -------------------------------------------------------------------------------------
// | 14-Apr-13 | HaiNM      | First creation.                                          |
// -------------------------------------------------------------------------------------
//
#endregion

#region Record of Audit
//             							REVIEW HISTORY
// -------------------------------------------------------------------------------------
// |   DATE    | REVIEWER   | NOTES                                                    |
// -------------------------------------------------------------------------------------
// -------------------------------------------------------------------------------------
//
#endregion

#region Using directives
using System;
using System.Reflection;
using System.CodeDom.Compiler;

using Thn;
using System.Text;
#endregion

namespace Thn.Web.Razor
{
    /// <summary>
    /// 
    /// </summary>
#if DEBUG || TESTING
    public
#else
    public
#endif
 class RazorCompileResults
    {
        #region Fields
        CompilerResults mResults;
        #endregion

        #region Properties

        #region Is Successful
        private bool mIsSuccessful;
        /// <summary>
        /// Check whether the operation was successful.
        /// </summary>
        public bool IsSuccessful
        {
            get { return mIsSuccessful; }
        }
        #endregion

        #region Errors
        /// <summary>
        /// Gets collection of errors.
        /// </summary>
        public CompilerErrorCollection Errors
        {
            get { return mResults.Errors; }
        }
        #endregion

        #region Error Message
        private string mErrorMessage;
        /// <summary>
        /// Gets errors as a text message.
        /// </summary>
        public string ErrorMessage
        {
            get { return mErrorMessage; }
        }
        #endregion
        
        #region Generated Codes
        internal string mGeneratedCodes;
        /// <summary>
        /// Gets generated codes as text.
        /// </summary>
        public string GeneratedCodes
        {
            get { return mGeneratedCodes; }
        }
        #endregion

        #region Compiled Assembly
        /// <summary>
        /// Gets reference to compiled assembly.
        /// </summary>
        public Assembly CompiledAssembly
        {
            get { return mResults.CompiledAssembly; }
        }
        #endregion

        #region Compiled Type
        private Type mCompiledType;
        /// <summary>
        /// Gets the class type after compilation.
        /// </summary>
        public Type CompiledType
        {
            get { return mCompiledType; }
        }
        #endregion
        
        #endregion

        #region Set Results
        internal void SetResults(CompilerResults results)
        {
            if (!results.Errors.HasErrors)
            {
                mIsSuccessful = true;
            }
            else
            {
                var compileErrors = new StringBuilder();
                foreach (System.CodeDom.Compiler.CompilerError compileError in results.Errors)
                    compileErrors.Append(String.Format("Line: {0}\t Col: {1}\t Error: {2}", compileError.Line, compileError.Column, compileError.ErrorText));
                mErrorMessage = compileErrors.ToString();
            }
            mResults = results;
        }
        #endregion

        #region Set Compiled Type
        internal void SetCompiledType(string fullname)
        {
            foreach (var item in CompiledAssembly.GetTypes())
            {
                if (item.FullName == fullname)
                {
                    mCompiledType = item;
                    break;
                }
            }
        }
        #endregion

        #region Constructors
        /// <summary>
        /// Create a new instance.
        /// </summary>
        public RazorCompileResults()
            : base()
        { }
        #endregion
    }
}
